package com.chartdemo;

import org.achartengine.chart.PieChart;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;

import com.chartdemo.data.DataFromJson;
import com.chartdemo.data.JsonStr;
import com.chartdemo.view.PieChartView;

public class PieChartDemo extends Activity
{
	private PieChartView layout;
	private DefaultRenderer renderer;
	private PieChart mChart;
	private DataFromJson mdata;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		init();
		layout = new PieChartView(this, mChart);
		setContentView(layout);
		setTitle(R.string.pie);
	}
	
	public void init()
	{
		mdata = new DataFromJson();
		mdata.pieData(JsonStr.comPercentStr);
		double[] values = mdata.getPieValue();
		// double[] values = new double[] { 12, 14, 11, 10, 19, 20 };
		int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN,
				Color.RED };
		
		int[] mcolors = new int[values.length];
		
		for (int i = 0; i < values.length; i++)
		{
			mcolors[i] = colors[i % colors.length];
		}
		
		renderer = buildCategoryRenderer(mcolors);
		renderer.setZoomButtonsVisible(true);
		renderer.setZoomEnabled(true);
		renderer.setChartTitleTextSize(20);
		renderer.setDisplayValues(true);
		
		CategorySeries dataset = buildCategoryDataset("Project budget", values);
		
		mChart = new PieChart(dataset, renderer);
	}
	
	/**
	 * Builds a category renderer to use the provided colors.
	 * 
	 * @param colors the colors
	 * @return the category renderer
	 */
	protected DefaultRenderer buildCategoryRenderer(int[] colors)
	{
		DefaultRenderer renderer = new DefaultRenderer();
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		renderer.setMargins(new int[] { 20, 30, 15, 0 });
		for (int color : colors)
		{
			SimpleSeriesRenderer r = new SimpleSeriesRenderer();
			r.setColor(color);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}
	
	/**
	 * Builds a category series using the provided values.
	 * 
	 * @param titles the series titles
	 * @param values the values
	 * @return the category series
	 */
	protected CategorySeries buildCategoryDataset(String title, double[] values)
	{
		CategorySeries series = new CategorySeries(title);
		int k = 0;
		String[] name = (String[]) mdata.getPieName();
		for (int i = 0; i < values.length; i++)
		{
			series.add(name[i], values[i]);
		}
		// for (double value : values)
		// {
		// series.add(, value);
		// // series.add("userCount " + ++k, value);
		// }
		
		return series;
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}
}
